import { createRouter, createWebHistory } from "vue-router";
import { getAddress } from "../api/getAddress";
import { Dialog } from "vant";
import "vant/es/dialog/style"; // 引入弹窗样式
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: "/",
      name: "home",
      component: () => import("../views/home/index.vue"),
      redirect: "/films/now-playing",
      children: [
        {
          path: "/films",
          name: "films",
          component: () => import("../views/home-films/index.vue"),
          children: [
            {
              path: "now-playing",
              name: "now-playing",
              component: () => import("../views/home/now-playing/index.vue"),
            },
            {
              path: "coming-soon",
              name: "coming-soon",
              component: () => import("../views/home/coming-soon/index.vue"),
            },
          ],
        },
        {
          path: "cinemas",
          name: "cinemas",
          component: () => import("../views/home-cinemas/index.vue"),
        },
        {
          path: "/active",
          name: "active",
          component: () => import("../views/home-active/index.vue"),
        },
        {
          path: "/center",
          name: "center",
          component: () => import("../views/home-center/index.vue"),
        },
      ],
    },
    {
      path: "/login",
      name: "login",
      component: () => import("../views/login/index.vue"),
    },
    {
      path: "/register",
      name: "register",
      component: () => import("../views/register/index.vue"),
    },
    {
      path: "/film/:id",
      name: "film",
      component: () => import("../views/film/index.vue"),
      children: [
        {
          path: "cinemas",
          component: () => import("../views/film/buyTicket/index.vue"),
        },
      ],
    },
    {
      path: "/city",
      name: "city",
      component: () => import("../views/city/index.vue"),
    },
    {
      path: "/cinemas-search",
      name: "cinemas-search",
      component: () => import("../views/home-cinemas/cinemas-search/index.vue"),
    },
  ],
});

if (!localStorage.getItem("city")) {
  router.beforeEach((to, from, next) => {
    //路由前置守卫 在加载前获取当前地址
    getAddress().then((res) => {
      if (res.includes("省")) {
        var parts = res.split("省");
      } else if (res.includes("自治区")) {
        var parts = res.split("自治区");
      }
      if (parts.length > 1) {
        var city = parts[1].split("市")[0];
        // console.log(city); // 输出：崇左
      } else {
        console.log("未找到城市名称");
      }
      Dialog.confirm({
        title: "提示",
        message: "是否切换到当前城市:  " + city,
      })
        .then(() => {
          //确认
          localStorage.setItem("city", city);
        })
        .catch(() => {
          //取消
          localStorage.setItem("city", "city");
          router.push("/city");
        });
      next();
    });
  });
}

export default router;
